package com.foreknow.service.impl;

import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    private GuestbookDaoImpl dao = new GuestbookDaoImpl();

    @Override
    public boolean add(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        boolean isRigth = false;
        try {
            isRigth = dao.save(gb);
            //dml操作需要提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }

    @Override
    public List<Guestbook> getQueryAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        List<Guestbook> list = new ArrayList<>();
        try {
            List<Object> objects = dao.getQueryAll();
            for (Object o :
                    objects) {
                Guestbook business = (Guestbook) o;
                list.add(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        boolean b = false;
        try {
            b = dao.deleteById(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return b;
    }

    @Override
    public int Update(Guestbook guestbook, int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        int i = 0;
        try {
            i = dao.Update(guestbook, id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public Guestbook getQueryById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        Guestbook guestbook = null;
        try {
            guestbook = dao.getQueryById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return guestbook;
    }

    @Override
    public Pagination getByPage(int pageSize, int pageNumber) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        Pagination pagination = null;
        try {
            pagination = dao.getGuestbookByPage(pageSize, pageNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pagination;
    }
}
